<template>
    <div class="frame_container" >
        <div class="container">
            <div class="image_container">
                <img src="../../public/img/logo.jpg">
            </div>
            <el-form :rules="rules" ref="loginFrom" :model="loginForm" class="demo-ruleForm">

                <el-form-item label="ID" label-width="100px"  prop="username">
                    <!--auto-complete  自动补全-->
                    <el-input  style="width: 300px;float: left" prefix-icon="el-icon-user-solid" type="text" auto-complete="false" v-model="loginForm.id" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码" label-width="100px" prop="password">
                    <el-input type="password" auto-complete="false" prefix-icon="el-icon-lock" style="width: 300px;float: left" v-model="loginForm.password" placeholder="输入密码"></el-input>
                </el-form-item>
                <el-form-item style="position: absolute;left: 35%">
                    <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
                    <el-button  @click="resetForm">重置</el-button>
                </el-form-item>

            </el-form>
        </div>
    </div>
</template>

<script>
    import {postRequest} from "../utils/api";
    import axios from 'axios'
    import requset from "../utils/request"

    export default {
        name: "Login-class",
        data() {
            return {

                loginForm: {
                    id: '',
                    password: ''
                },
                checked: true,
                rules: {
                    id: [
                        { required: true, message: '请输入学生学号', trigger: 'blur' },
                        { min: 6, message: '11位学号', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        {min:3, max:8, message: '长度在 3 到 5 个字符',trigger: 'blur'}
                    ]

                }
            }
        },
        methods: {
            submitLogin(){
                this.$refs.loginFrom.validate((valid) => {
                    if(valid){
                        postRequest('/Login/login',this.loginForm).then(resp=>{

                            if (resp) {
                                //存储用户token
                                const tokenStr ="" +resp.obj.token;
                                window.sessionStorage.setItem('tokenStr',tokenStr)
                                window.sessionStorage.setItem('token', tokenStr);
                                this.$store.commit('initRoutes',resp.obj.user.user_id)
                                this.$store.commit('setToken',tokenStr)
                                localStorage.setItem("token",tokenStr)

                                this.$router.push('/main');

                            }
                        })


                    }else{
                        this.$message.error("请");
                        return false;
                    }

                });
            },
            resetForm(){
                this.loginForm.id='';
                this.loginForm.password=''
            }

        }
    }
</script>

<style scoped>
    .frame_container{
        height: 100%;
        background-color: cornflowerblue;
    }
    .container{
        height: 320px;
        width: 450px;
        position: absolute;
        border-radius: 15%;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        box-shadow:  0 2px 12px 0 rgba(0, 0, 0, 0.1);
        background-color: #ffffff;
    }
    form{
        position: relative;
        top: 100px;
    }
    img{
        height: 130px;
        width: 130px;
        border: 1px solid #eeeeee;
        background-color: #ffffff;
        border-radius: 130px;
        padding: 10px;
        box-shadow:  0 2px 12px 0 rgba(0, 0, 0, 0.1);
        margin: -65px auto;
        position: relative;
        left: 150px;
    }
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6 ;
    }
    .loginTitle {
        margin: 0px auto 40px auto;
        text-align: center ;
    }
    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px  ;
    }
    .el-form-item__content {
        display: flex;
        align-items: center;
    }
</style>
